﻿using System.ComponentModel;

namespace Borg.Framework
{
    public class PlugSelectionController : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public PlugSelectionController(SlotInfo slot, SelectionChangedAction action, SelectionIndexProvider selector)
        {
            Slot = slot;

            Slot.SelectionChanged += SelectionChanged;

            Action = action;
            Selector = selector;
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedIndex"));
        }

        private SelectionChangedAction Action { get; set; }

        public int SelectedIndex
        {
            get { return Selector == null ? -1 : Selector(Slot); }
            set
            {
                if (value != -1 && Action != null)
                    Action(Slot, value);
            }
        }

        private SelectionIndexProvider Selector { get; set; }

        private SlotInfo Slot { get; set; }
    }
}

